<?php

namespace Addons\Qrcode\Controller;
use Home\Controller\AddonsController;

class QrcodeController extends AddonsController{

    function _initialize() {
        $this->model = $this->getModel ( 'qr_code' );
        parent::_initialize ();
    }

    //保存二维码到lq_qr_code表
    public function qr_code_save($qr_code,$ewmid,$extra_text,$type){
        $data['token'] =  get_token ();
        $data['qr_code']=$qr_code;
        $data['scene_id']=$ewmid;
        $data['action_name']=$type;
        $data['aim_id']=109;
        $data['extra_text']=$extra_text;
        $data['addon']='Qrcode';
        $data['cTime']= time() ;
        $Model = M('qr_code');
        $Model->data($data)->add();
    }



// 通用插件的列表模型
    public function lists() {

        $token = get_token ();
        $list_data = $this->_get_model_list ( $this->model );
        $this->assign ( $list_data );
        $this->display ();

    }

    // 通用插件的编辑模型
    public function edit() {

        parent::common_edit ( $this->model );
    }



    // 通用插件的删除模型
    public function del() {
        parent::common_del ( $this->model );
    }





    function send_ewm() {


        //-----获取access_token
        //建议weiphp以后把这块封装成函数 并把access_token写入wp_member_public表中
        $map ['token'] = get_token ();
        $info = M ( 'member_public' )->where ( $map )->find ();
        $url_get = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $info ['appid'] . '&secret=' . $info ['secret'];

        $ch1 = curl_init ();
        $timeout = 5;
        curl_setopt ( $ch1, CURLOPT_URL, $url_get );
        curl_setopt ( $ch1, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch1, CURLOPT_CONNECTTIMEOUT, $timeout );
        curl_setopt ( $ch1, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $ch1, CURLOPT_SSL_VERIFYHOST, false );
        $accesstxt = curl_exec ( $ch1 );
        curl_close ( $ch1 );
        $access = json_decode ( $accesstxt, true );
        if (empty ( $access ['access_token'] )) {
            $this->error ( '获取access_token失败,请确认AppId和Secret配置是否正确,然后再重试。' );
        }

        //-----获取access_token结束


        //----生成二维码
        //$ewmid = I('post.ewmid',0);
        $ewmid = I('ewmid');
        $type=I('type');
        $extra_text=I('extra_text');
        $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=' . $access ['access_token'];
        $header [] = "content-type: application/x-www-form-urlencoded; charset=UTF-8";

        $qrcode=array(
            'action_name'=>$type,
            'action_info'=>array(
                'scene'=>array(
                    'scene_id'=>$ewmid
                )
            )
        );
        if(strcmp($type,'QR_SCENE')==0){
            $qrcode['expire_seconds']=604800;
        }
        $qrcode=json_encode($qrcode);
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
        curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)' );
        curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $qrcode );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        $res = curl_exec ( $ch );
        curl_close ( $ch );
        $res = json_decode ( $res, true );

        if ($res ['errcode'] == 0) {


            $ticket = $res ['ticket'];
            $url= 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.UrlEncode($ticket) ;

            $qr_code=$url;
            //--------------保存二维码到lq_qr_code表
            $this->qr_code_save($qr_code,$ewmid,$extra_text,$type);


            $this->success ( '生成二维码成功');
        } else {
            $this->success ( '生成二维码失败，错误的返回码是：' . $res ['errcode'] . ', 错误的提示是：' . $res ['errmsg'] );
        }
    }

}
